package com.ssau.expert.core.impl;

import com.ssau.expert.app.DatabaseAPI;
import com.ssau.expert.common.QuestionRequestListener;
import com.ssau.expert.common.SuggestedResult;
import com.ssau.expert.core.api.DatabaseCoreAPI;
import com.ssau.expert.core.api.ExpertCoreAPI;
import com.ssau.expert.exception.NotInitializedException;
import com.ssau.expert.exception.RuleParseException;
import com.ssau.expert.exception.SemanticException;
import com.ssau.expert.util.Names;
import com.ssau.expert.util.QuestionHelper;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Author: Sergey Saiyan
 * Created: 05.09.13 12:04
 */
public class ExpertSystem implements ExpertCoreAPI {

    private State state = State.NOT_INITIALIZED;

    protected QuestionRequestListener listener = null;

    @Override
    public void uploadFileWithRules(File file) throws IOException, RuleParseException, SemanticException {
        RuleParser.uploadRules(file);
    }

    @Override
    public void initExpertSystem() throws NotInitializedException {
        if (database.getRules().isEmpty()) {
            throw new NotInitializedException("Rules were not initialized");
        }

        try {
            QuestionHelper.initQuestions();
        } catch (IOException e) {
            throw new NotInitializedException("Can't init questions. Check template paths.");
        }

        state = State.INITIALIZED;
    }

    @Override
    public void setQuestionRequestListener(QuestionRequestListener listener) {
        this.listener = listener;
    }

    @Override
    public QuestionRequestListener getListener() {
        return listener;
    }

    @Override
    public SuggestedResult getResult() {
        return SuggestHelper.get().doSuggestion();
    }

    @Override
    public DatabaseAPI getDatabase() {
        return database;
    }

    @Override
    public List<String> getLog() {
        return Logger.get().getLog();
    }

    @Override
    public String getShipProfileAnswer() {
        String value = SuggestHelper.get().table.getVariable(Names.PROFILE).getValue();
        return value != null ? value : "";
    }

    @Override
    public String getStaffAnswer() {
        String value = SuggestHelper.get().table.getVariable(Names.STUFF).getValue();
        return value != null ? value : "";
    }

    @Override
    public String getDistanceAnswer() {
        String value = SuggestHelper.get().table.getVariable(Names.DISTANCE).getValue();
        return value != null ? value : "";
    }

    @Override
    public void setState(State state) {
        this.state = state;
    }

    @Override
    public State getState() {
        return state;
    }

    public static ExpertSystem get() {
        return instance;
    }

    private ExpertSystem() {
    }

    private static ExpertSystem instance = new ExpertSystem();
    private DatabaseCoreAPI database = DatabaseImpl.get();

    public enum State {
        INITIALIZED,
        NOT_INITIALIZED,
        WAITING_INPUT,
        FINISHED
    }
}
